function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 加载更多
function loadMore(that,hasMoreData,url){
  var curPage = that.data.curPage;
  if(hasMoreData == true){
    that.setData({
      hasMoreData: false
    })
  }

  wx.request({
    url: url,
    header: {
        'content-type': 'application/json'
    },
    data: {
      curPage: curPage,
      busId: parseInt(wx.getStorageSync("busId")),
      employeesId: parseInt(wx.getStorageSync("employeesId")),
      schoolId: parseInt(wx.getStorageSync("schoolId"))
    },
    success: function(res){
      console.log("分页请求返回的数据==",res.data);
      var pageCount = res.data.data.pageCount;
      var code = res.data.code;
      if(code == 100){
        var list = res.data.data.subList;
        var listSize= list.length;
        var startDate =  list[0].startDate;
        var updateDate = list[0].updateDate;
        if(startDate){
          for(var i = 0; i < list.length; i++){
              list[i].startDate =formatTime( new Date(list[i].startDate));
          }
        }
        if(updateDate){
          for(var i = 0; i < list.length; i++){
            list[i].updateDate =formatTime( new Date(list[i].updateDate));
          }
        }
        if(curPage > pageCount){
          that.setData({
            hasMoreData: false
          });
          return
        }
        that.setData({
          dataList: that.data.dataList.concat(list),
          curPage: curPage+1,
          hasMoreData: true
        });
        console.log("curpage===",that.data.curPage);
      }
      else{
        that.setData({
          hasMoreData: false
        })
        return
      }
      
    }
  })
};

//登录
function login(app, callback) {
  wx.login({ //login流程
    success: function (res) { //登录成功
      if (res.code) {
        var code = res.code;
        wx.getUserInfo({
          success: function (res1) {
            console.log("获取用户信息成功");
            wx.request({
              url: app.globalData.domain + '/wxapplet/79B4DE7C/applet_grant.do',
              //url: app.globalData.http + 'login.do',
              data: {
                busId: app.globalData.busId,
                js_code: code,
                appid: 'wxc71d3e2f77e4109f',//app.globalData.appid
                rawData: res1.rawData,
                style: app.globalData.style
              },
              method: 'GET',
              header: {
                'content-type': 'application/json'
              },
              success: function (res2) {
                //if (res2.data.success) {
                if (!res2.data.success) {
                  console.log(res2.data);
                  //登录成功
                  //wx.setStorageSync('phone', res2.data.member.phone);
                  //wx.setStorageSync('memberId', res2.data.memberId);
                  wx.setStorageSync('phone', 15017934717);
                  wx.setStorageSync('memberId', 1225447);
                  wx.setStorageSync('appid', app.globalData.appid);
                  wx.setStorageSync('isAdvert', res2.data.isAdvert);
                  wx.setStorageSync('userInfo', res1.userInfo);
                  if (callback) callback(res2);
                } else {
                  wx.showModal({
                    title: '提示',
                    content: '授权失败!',
                    showCancel: false,
                  })
                }
              }
            })
          },
          fail: function (e) {
            wx.showModal({
              title: '提示',
              content: '获取用户信息失败！',
              showCancel: false,
            })
          }
        })
      }
    },
    fail: function () {
      wx.showModal({
        title: '提示',
        content: '登录失败！',
        showCancel: false,
      })
    }
  });

}

module.exports = {
  formatTime: formatTime,
  loadMore: loadMore,
  login: login
}
